/*
OpenMVL Matrix Vector Library
Copyright (c) 2009 FluidInteractive (R)
 
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising
from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
 
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
 
Written by: Nicola Candussi <nicola@fluidinteractive.com>
*/

//expr_trans.hpp

#ifndef MVL_EXPR_TRANS_HPP
#define MVL_EXPR_TRANS_HPP

#include "layout_trans.hpp"
#include "expr.hpp"

namespace mvl {

template<typename E>
class expr_trans : public expr< expr_trans<E> > 
{ 
public:
    typedef expr_trans  self_t;
    typedef E           expr_t;
    typedef typename meta::if_<
        typename meta::is_const<expr_t>::type,
        typename expr_t::const_closure_t,
        typename expr_t::closure_t
    >::type expr_closure_t;

public:
    typedef typename layout::layout_trans<typename expr_t::layout_t>::type layout_t;
    typedef typename expr_t::value_type      value_type;
    typedef typename layout_t::order    order;
    typedef typename layout_t::Rows     Rows;
    typedef typename layout_t::Cols     Cols;
    typedef typename layout_t::Size     Size;

    typedef self_t closure_t;
    typedef const self_t const_closure_t;

private:
    template<typename Entry, typename Tag> struct get_src : Entry::src {};
    template<typename Entry> struct get_src<Entry, layout::dense_layout_tag> : 
        layout::entry<typename Entry::j, typename Entry::i, typename Entry::tag> {};

public:

    expr_trans(expr_t& e) : m_e(e) { }

    //assign
    self_t& operator = (self_t const& rhs) {
        mat_mat_assign<self_t, self_t, fn_assign<value_type, value_type> >::apply(*this, rhs);
        return *this;
    }

    template<typename E2>
    self_t& operator = (expr<E2> const& rhs) {
        MVL_STATIC_CHECK((E2::Rows::value == Rows::value), "mat_size_mismatch");
        MVL_STATIC_CHECK((E2::Cols::value == Cols::value), "mat_size_mismatch");
        mat_mat_assign<self_t, E2, fn_assign<value_type, value_type> >::apply(*this, rhs());
        return *this;
    }

    //access
    template<typename Entry>
    value_type at() const {
        return m_e.template at<typename get_src<Entry, typename layout_t::tag>::type>();
    }

    template<typename Entry>
    value_type& at() {
        return m_e.template at<typename get_src<Entry, typename layout_t::tag>::type>();
    }

private:
    expr_closure_t m_e;
};

template<typename E>
struct trans_traits 
{
    typedef expr_trans<E> type;
};

template<typename E> 
inline 
const typename trans_traits<const E>::type
trans(expr<E> const& e) {
    typedef const typename trans_traits<const E>::type expr_t;
    return expr_t(e());
}

template<typename E> 
inline 
typename trans_traits<E>::type
trans(expr<E>& e) {
    typedef typename trans_traits<E>::type expr_t;
    return expr_t(e());
}

} // namespace mvl


#endif
